package front_article_api

import (
	"blog_go/global"
	"blog_go/models"
	"blog_go/models/res"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"github.com/sirupsen/logrus"
)

type ArticleDetailsReq struct {
	ID uint `json:"id" form:"id" binding:"required"`
}

type ArticleDetailsRsp struct {
	models.ArticleModel
}

func (ArticleApi) ArticleDetails(c *gin.Context) {
	var request ArticleDetailsReq
	if err := c.ShouldBindQuery(&request); err != nil {
		logrus.Error(err)
		panic(err)
	}
	var articleModel models.ArticleModel
	err := global.DB.Preload("TagModels").First(&articleModel, request.ID).Error
	if err != nil {
		logrus.Error(err)
		panic(err)
	}
	var articleDetailsResponse ArticleDetailsRsp
	err = copier.Copy(&articleDetailsResponse, &articleModel)
	if err != nil {
		logrus.Error(err)
		panic(err)
	}
	res.OkWithDate(articleDetailsResponse, c)
}
